
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

part  "JsonSerialize.json_serializable.g.part";

///这个标注是告诉生成器，这个类是需要生成Model类的
@JsonSerializable()

class JsonSerialize{
  JsonSerialize(this.name, this.email);

  String name;
  @JsonKey(name: 'email')
  String email;
  //不同的类使用不同的mixin即可
  factory JsonSerialize.fromJson(Map<String, dynamic> json) => _$JsonSerializeFromJson(json);
  Map<String, dynamic> toJson() => _$JsonSerializeToJson(this);
}

void main() {
  //一个JSON格式的用户列表字符串
  String jsonStr = '[{"name":"Jack"},{"name":"Rose"}]';
  //将JSON字符串转为Dart对象(此处是List)
  List items = json.decode(jsonStr);
  jsonStr = '''
  {
    "name": "John Smith",
    "email": "john@example.com"
  }
  ''';
  Map<String, dynamic> userMap = json.decode(jsonStr);
  JsonSerialize user = JsonSerialize.fromJson(userMap);
  print('Howdy, ${user.name}!');
  print('We sent the verification link to ${user.email}.');
  var jsons = user.toJson();
  print(jsons);
  print(json.runtimeType);
  var jsonstr = json.encode(user);
  print(jsonstr);
  print(jsonstr.runtimeType);
}
